package com.nanhai.buddhist.college.module.collegebase.controller.admin.classes;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.vo.ClassesStudentListPageVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classes.vo.ClassesCreateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classes.vo.ClassesExcelVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classes.vo.ClassesPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classes.vo.ClassesRespVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classes.vo.ClassesUpdateReqVO;
import com.nanhai.buddhist.college.module.collegebase.convert.classes.ClassesConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classes.ClassesDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentroll.StudentRollDO;
import com.nanhai.buddhist.college.module.collegebase.service.classes.ClassesService;
import com.nanhai.buddhist.college.module.collegebase.service.studentroll.StudentRollService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.error;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.CLASSES_NOT_DELETE;

@Api(tags = "管理后台 - 学校管理-班级")
@RestController
@RequestMapping("/collegebase/classes")
@Validated
public class ClassesController {

    @Resource
    private ClassesService classesService;

    @Resource
    private StudentRollService studentRollService;

    @PostMapping("/create")
    @ApiOperation("创建学校管理-班级")
    @PreAuthorize("@ss.hasPermission('collegebase:college_manager')")
    public CommonResult<String> createClasses(@Valid @RequestBody ClassesCreateReqVO createReqVO) {
        return success(classesService.createClasses(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新学校管理-班级")
    @PreAuthorize("@ss.hasPermission('collegebase:college_manager')")
    public CommonResult<Boolean> updateClasses(@Valid @RequestBody ClassesUpdateReqVO updateReqVO) {
        classesService.updateClasses(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除学校管理-班级")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:college_manager')")
    public CommonResult<Boolean> deleteClasses(@RequestParam("id") String id) {
        ClassesStudentListPageVO pageVO = new ClassesStudentListPageVO();
        pageVO.setId(id);
        pageVO.setPageNo(1);
        pageVO.setPageSize(10);
        PageResult<StudentRollDO> page = studentRollService.getStudentRespDTOByClassesId(pageVO);
        if (page != null && page.getList().size() > 0) {
            return error(CLASSES_NOT_DELETE);
        }

        classesService.deleteClasses(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得学校管理-班级")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public CommonResult<ClassesRespVO> getClasses(@RequestParam("id") String id) {
        ClassesDO classes = classesService.getClasses(id);
        return success(ClassesConvert.INSTANCE.convert(classes));
    }

    @GetMapping("/list")
    @ApiOperation("获得学校管理-班级列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<ClassesRespVO>> getClassesList(@RequestParam("ids") Collection<String> ids) {
        List<ClassesDO> list = classesService.getClassesList(ids);
        return success(ClassesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得学校管理-班级分页")
    @PreAuthorize("@ss.hasPermission('collegebase:college_manager')")
    public CommonResult<PageResult<ClassesRespVO>> getClassesPage(@Valid ClassesPageReqVO pageVO) {
        PageResult<ClassesDO> pageResult = classesService.getClassesPage(pageVO);
        return success(ClassesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出学校管理-班级 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:college_manager')")
    @OperateLog(type = EXPORT)
    public void exportClassesExcel(@Valid ClassesPageReqVO exportReqVO,
                                   HttpServletResponse response) throws IOException {
        exportReqVO.setPageNo(1);
        exportReqVO.setPageSize(999999);
        PageResult<ClassesDO> pageResult = classesService.getClassesPage(exportReqVO);
        // 导出 Excel
        List<ClassesExcelVO> list = ClassesConvert.INSTANCE.convertList02(pageResult.getList());
        ExcelUtils.write(response, "学校管理-班级.xls", "数据", ClassesExcelVO.class, list);
    }

    @GetMapping("/byProfessions/list")
    @ApiOperation("根据专业获得班级列表")
    @ApiImplicitParam(name = "professionsId", value = "专业id", required = true, example = "1024,2048", dataTypeClass = String.class)
    public CommonResult<List<ClassesRespVO>> getClassesListByProfessionsId(@RequestParam("professionsId") String professionsId) {
        List<ClassesDO> list = classesService.getClassesListByProfessionsId(professionsId);
        return success(ClassesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/byDepartments/list")
    @ApiOperation("根据系部获得班级列表")
    @ApiImplicitParam(name = "departmentsId", value = "专业id", required = true, example = "1024,2048", dataTypeClass = String.class)
    public CommonResult<List<ClassesRespVO>> getClassesListByDepartmentsId(@RequestParam("departmentsId") String departmentsId) {
        List<ClassesDO> list = classesService.getClassesListByDepartmentsId(departmentsId);
        return success(ClassesConvert.INSTANCE.convertList(list));
    }

}
